//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.dstz.bpm.engine.action.handler.task;

import cn.hutool.core.collection.CollectionUtil;
import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.core.util.StringUtil;
import com.dstz.bpm.api.constant.ActionType;
import com.dstz.bpm.api.constant.NodeType;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.api.exception.WorkFlowException;
import com.dstz.bpm.api.model.def.NodeProperties;
import com.dstz.bpm.api.model.inst.BpmExecutionStack;
import com.dstz.bpm.api.model.nodedef.BpmNodeDef;
import com.dstz.bpm.api.model.task.IBpmTask;
import com.dstz.bpm.api.service.BpmProcessDefService;
import com.dstz.bpm.core.manager.BpmTaskOpinionManager;
import com.dstz.bpm.core.manager.BpmTaskStackManager;
import com.dstz.bpm.core.model.BpmTask;
import com.dstz.bpm.core.model.BpmTaskOpinion;
import com.dstz.bpm.core.model.BpmTaskStack;
import com.dstz.bpm.engine.action.cmd.DefualtTaskActionCmd;
import com.dstz.bpm.engine.model.BpmIdentity;
import com.dstz.sys.api.model.SysIdentity;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class TaskRejectActionHandler extends AbstractTaskActionHandler<DefualtTaskActionCmd> {
   private static Logger log = LoggerFactory.getLogger(TaskRejectActionHandler.class);
   @Resource
   BpmTaskStackManager k;
   @Resource
   BpmTaskOpinionManager aL;
   @Resource
   BpmProcessDefService aM;

   public TaskRejectActionHandler() {
   }


   // 1
   @Override
   public void h(DefualtTaskActionCmd actionModel) {
      NodeProperties nodeProperties = this.a.getBpmNodeDef(actionModel.getDefId(), actionModel.getNodeId()).getNodeProperties();
      String destinationNode = this.a(actionModel, nodeProperties);
      if ("history".equals(nodeProperties.getBackUserMode())) {
         this.a(actionModel, destinationNode);
      }

      IBpmTask task = actionModel.getBpmTask();
      if (task.getNodeId().equals(destinationNode)) {
         throw new BusinessException("目标节点不能为当前任务节点", BpmStatusCode.CANNOT_BACK_NODE);
      } else {
         actionModel.setDestination(destinationNode);
         log.info("任务【{}-{}】将驳回至节点{}", new Object[]{task.getName(), task.getId(), destinationNode});
      }
   }

   private void a(DefualtTaskActionCmd actionModel, String destinationNode) {
      SysIdentity identitys = null;
      List<BpmTaskOpinion> taskOpinions = this.aL.getByInstAndNode(actionModel.getInstanceId(), destinationNode);
      Iterator var5 = taskOpinions.iterator();

      while(var5.hasNext()) {
         BpmTaskOpinion opinion = (BpmTaskOpinion)var5.next();
         if (StringUtil.isNotEmpty(opinion.getApprover())) {
            identitys = new BpmIdentity(opinion.getApprover(), opinion.getApproverName(), "user");
         }
      }

      if (identitys != null) {
         List<SysIdentity> list = new ArrayList();
         list.add(identitys);
         actionModel.setBpmIdentity(destinationNode, list);
      }

   }

   protected String a(DefualtTaskActionCmd actionModel, NodeProperties nodeProperties) {
      if (StringUtil.isNotEmpty(actionModel.getDestination())) {
         return actionModel.getDestination();
      } else if (nodeProperties != null && StringUtil.isNotEmpty(nodeProperties.getBackNode())) {
         return nodeProperties.getBackNode();
      } else {
         List<BpmTaskStack> stackList = this.k.getByInstanceId(actionModel.getInstanceId());
         BpmTaskStack currentStack = null;

         for(int i = stackList.size() - 1; i > -1; --i) {
            BpmTaskStack stack = (BpmTaskStack)stackList.get(i);
            if (stack.getTaskId().equals(actionModel.getTaskId())) {
               currentStack = stack;
               break;
            }
         }

         if (currentStack == null) {
            throw new BusinessException(actionModel.getTaskId() + " currentStack lost 堆栈信息异常 ");
         } else {
            BpmNodeDef nodeDef = this.aM.getBpmNodeDef(actionModel.getDefId(), currentStack.getNodeId());
            if (nodeDef.getIncomeNodes().size() == 1 && ((BpmNodeDef)nodeDef.getIncomeNodes().get(0)).getType() == NodeType.USERTASK) {
               return ((BpmNodeDef)nodeDef.getIncomeNodes().get(0)).getNodeId();
            } else {
               BpmExecutionStack preStack = this.a(stackList, currentStack.getParentId());
               if (preStack == null) {
                  throw new WorkFlowException("上一任务处理记录查找失败，无法驳回！", BpmStatusCode.NO_BACK_TARGET);
               } else {
                  return preStack.getNodeId();
               }
            }
         }
      }
   }

   BpmExecutionStack a(List<BpmTaskStack> stackList, String id) {
      String parentId = id;

      for(int i = stackList.size() - 1; i > -1; --i) {
         BpmTaskStack stack = (BpmTaskStack)stackList.get(i);
         if (stack.getId().equals(parentId)) {
            parentId = stack.getParentId();
            if ("userTask".equals(stack.getNodeType())) {
               return stack;
            }
         }
      }

      return null;
   }

   // 1
   @Override
   public void i(DefualtTaskActionCmd actionModel) {
      NodeProperties nodeProperties = this.a.getBpmNodeDef(actionModel.getDefId(), actionModel.getNodeId()).getNodeProperties();
      if ("back".equals(nodeProperties.getBackMode())) {
         List<BpmTask> tasks = this.aJ.getByInstIdNodeId(actionModel.getInstanceId(), actionModel.getDestination());
         if (CollectionUtil.isEmpty(tasks)) {
            throw new WorkFlowException(String.format("任务[%s]返回节点[%s]标记失败，待标记任务查找不到", actionModel.getDestination(), actionModel.getNodeId()), BpmStatusCode.NO_BACK_TARGET);
         }

         boolean hasUpdated = false;
         Iterator var5 = tasks.iterator();

         while(var5.hasNext()) {
            BpmTask task = (BpmTask)var5.next();
            if (StringUtil.isNotEmpty(task.getActInstId()) && StringUtil.isNotEmpty(task.getTaskId())) {
               if (hasUpdated) {
                  throw new WorkFlowException("任务返回节点标记失败，期望查找一条，但是出现多条", BpmStatusCode.NO_BACK_TARGET);
               }

               task.setBackNode(actionModel.getNodeId());
               this.aJ.update(task);
               hasUpdated = true;
            }
         }

         if (!hasUpdated) {
            throw new WorkFlowException("任务返回节点标记失败，待标记任务查找不到", BpmStatusCode.NO_BACK_TARGET);
         }
      }

   }

   public ActionType getActionType() {
      return ActionType.REJECT;
   }

   public int getSn() {
      return 3;
   }

   public String getConfigPage() {
      return "/bpm/task/taskOpinionDialog.html";
   }
}
